package controller;

import controller.Link;
import controller.link.game.Healing;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.game.Player;
import model.game.npcs.Innkeeper;
import model.Attendant;
import model.Server;

public class Waiting implements Runnable{

    private Thread thread;
    private Server server;
    private Attendant at;
    private Link link;
    private boolean running;
    private Player player;
    private int timeWaiting;
    private String name;

    public Waiting(Player player, Attendant at, Server sv, int timeWaiting, String name) {
        this.name = name;
        this.timeWaiting = timeWaiting;
        this.player = player;
        this.link = new Healing(player, name);
        this.at = at;
        this.server = sv;
        this.thread = new Thread(this, player.getName());
        at.setWaiting(this);
        running = true;
        thread.start();
        
    }
    public void stop(Attendant at){
        running = false;
        at.setWaiting(null);

    }
    @Override
    public void run() {
        if (link instanceof Healing) {
            Healing healing = (Healing) link;
            while (healing.notComplete()&& running) {
                link.serverUse(server, at);
                healing.setClientExecuting(true);
                try {
                    Thread.sleep(1000+timeWaiting);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Waiting.class.getName()).log(Level.SEVERE, null, ex);
                    break;
                }
            }
        }

    }

    public Thread getThread() {
        return thread;
    }
}
